<template>
  <div class="dialog">
    <el-dialog width="45%" :title="title" :visible="true" :close-on-click-modal="false" :before-close="close">
      <el-form ref="form" :model="form" :rules="rules" label-width="130px" :disabled="type === 'detail'">
        <el-form-item label="协议编号：">
          <el-col :span="12">
            <el-input v-model="form.agreementNo" placeholder="系统自动生成，无需填写" disabled />
          </el-col>
        </el-form-item>
        <el-form-item label="协议名称：" prop="agreementName">
          <el-col :span="12">
            <el-input v-model="form.agreementName" placeholder="请输入" clearable />
          </el-col>
        </el-form-item>
        <el-form-item label="协议类型：" prop="agreementType">
          <el-col :span="12">
            <el-select v-model="form.agreementType" placeholder="请选择" clearable>
              <el-option
                v-for="item in agreementType"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-col>
        </el-form-item>
        <el-form-item v-if="form.agreementType === 0" label="所属行业：" prop="industryCode">
          <el-col :span="12">
            <el-select v-model="form.industryCode" placeholder="请选择" clearable>
              <el-option
                v-for="item in industryList"
                :key="item.industryCode"
                :label="item.industryName"
                :value="item.industryCode"
              />
            </el-select>
          </el-col>
        </el-form-item>
        <el-form-item label="协议文件：" prop="agreementUrl">
          <el-col :span="12">
            <UploadFile :file-list="form.agreementUrl" :accept="'.pdf'" :file-type="'file'" @change="uploadHandle($event, 'agreementUrl')">
              <div slot="tip" class="upload-tip">只能上传PDF文件，且不超过500kb</div>
            </UploadFile>
          </el-col>
        </el-form-item>
      </el-form>
      <div v-if="type === 'add'" slot="footer">
        <el-button @click="close">取消</el-button>
        <el-button type="primary" :loading="loading" @click="confirm">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { addAgreement, getAgreementDetail } from '@/api/agreement'
import { getIndustryList } from '@/api/dictionary'
import UploadFile from '@/components/UploadFile/index.vue'
import { AGREEMENT_TYPE } from '@/utils/enum'
export default {
  name: 'AddAgreement',
  components: {
    UploadFile
  },
  props: {
    type: {
      type: String,
      default: 'add' // add:新增 detail:详情
    },
    id: {
      type: [String, Number],
      default: ''
    }
  },

  data() {
    return {
      loading: false,
      agreementType: AGREEMENT_TYPE,
      industryList: [],
      form: {
        agreementNo: '',
        agreementName: '',
        agreementType: '',
        industryCode: '',
        agreementUrl: '',
        originalName: ''
      },
      rules: {
        agreementName: [{ required: true, message: '请输入协议名称', trigger: ['blur'] }],
        agreementType: [{ required: true, message: '请选择协议类型', trigger: ['change'] }],
        industryCode: [{ required: true, message: '请选择所属行业', trigger: ['change'] }],
        agreementUrl: [{ required: true, message: '请上传协议文件', trigger: ['change'] }]
      }
    }
  },

  computed: {
    title() {
      if (this.type === 'add') {
        return '新增协议'
      } else {
        return '协议详情'
      }
    }
  },

  created() {
    this.getIndustryList()
    if (this.type === 'detail') {
      this.getDetail()
    }
  },

  methods: {

    // 获取行业列表
    getIndustryList() {
      getIndustryList().then(res => {
        this.industryList = res || []
      }).catch(() => {})
    },

    // 获取详情
    getDetail() {
      const data = {
        id: this.id
      }
      getAgreementDetail(data).then(res => {
        const { agreementNo, agreementName, agreementType, industryCode, agreementUrl } = res
        this.form = {
          agreementNo,
          agreementName,
          agreementType,
          industryCode,
          agreementUrl
        }
      }).catch(() => {
      })
    },

    // 取消
    close() {
      this.$emit('close')
    },

    // 确定
    confirm() {
      this.$refs.form.validate(valid => {
        if (valid) {
          this.loading = true
          const data = {
            ...this.form
          }
          addAgreement(data).then(() => {
            this.$message.success('新增成功')
            this.loading = false
            this.$emit('confirm')
            this.close()
          }).catch(() => {
            this.loading = false
          })
        } else {
          return false
        }
      })
    },

    // 上传图片处理
    uploadHandle(payload, name) {
      this.form[name] = payload.fileList[0]?.url
      this.form.originalName = payload.fileList[0]?.name
      this.$refs.form.validateField(name)
    }

  }

}
</script>

<style lang="scss" scoped>

.el-select{
  width: 100%;
}

</style>
